package com.woniuxy.intellihome.controller.ucenter;

import com.woniuxy.intellihome.entity.*;
import com.woniuxy.intellihome.service.OrderService;
import com.woniuxy.intellihome.service.OrderitemService;
import com.woniuxy.intellihome.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @author pengZhihong
 * @author jobob
 * @author pengZhihong
 * @since 2021-08-17
 * 1、实现ucenter index主页的子页面嵌套展示功能，通过左侧不同链接跳展示不同功能
 * @since 2021-08-16
 * 1、实现ucenter index首页跳转功能
 */
@Slf4j
@Controller("ucenter/user")
@RequestMapping("ucenter/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderitemService orderitemService;

    //跳转至ucenter首页
    @GetMapping("/index")
    public String list(HttpSession session, Model model) {
        //获取当前登录用户
//        User user = (User) session.getAttribute("user1");
        //通过user_account获取数据库中user信息
        //将user信息传至前台
//        user = userService.getByAccount("小黄");
//        model.addAttribute("user",userService.getByAccount("小黄"));
        //仅测试用
        User user = (User) session.getAttribute("user1");

        model.addAttribute("user", user);

        return "ucenter/User_Center";
    }

    //跳转至的修改密码页面
    @GetMapping("/password")
    public String goUpdatePwd(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user1");

        model.addAttribute("user", user);

        return "ucenter/user_Password";
    }

    @PostMapping("update")
    @ResponseBody
    public ResponseEntity update(HttpSession session, @RequestBody Password password) {
        ResponseEntity responseEntity = new ResponseEntity();
        User user = (User) session.getAttribute("user1");
        String pwd = user.getPassword();
        if (password.getCfmpwd().length() == 0 || password.getOldpwd().length() == 0 || password.getNewpwd().length() == 0) {
            responseEntity.setCode(204);
            responseEntity.setMsg("请输入数据");
            return responseEntity;
        }
        if (!pwd.equals(password.getOldpwd())) {
            responseEntity.setCode(201);
            responseEntity.setMsg("原密码错误");
            return responseEntity;
        }
        if (!password.getNewpwd().equals(password.getCfmpwd())) {
            responseEntity.setCode(202);
            responseEntity.setMsg("确认密码与修改后密码不一致");
            return responseEntity;
        }
        if (password.getNewpwd().length() < 6 || password.getNewpwd().length() > 16) {
            responseEntity.setCode(203);
            responseEntity.setMsg("密码长度要在6-16位");
            return responseEntity;
        }
        responseEntity.setCode(200);
        responseEntity.setMsg("修改成功");
        user.setPassword(password.getNewpwd());
        userService.updateById(user);
        return responseEntity;
    }

    //跳转个人信息中心
    @GetMapping("info")
    public String jumpInfo() {
        return "ucenter/user_info";
    }

    //用户信息中心获取一个user对象
    @GetMapping("getmyuser")
    @ResponseBody
    public ResponseEntity<User> getUser(HttpSession session) {
        User user = (User) session.getAttribute("user1");
        return new ResponseEntity<>(user);
    }

    //用户信息更新
    @RequestMapping("updateUser")
    @ResponseBody
    public ResponseEntity checkName(@RequestBody User myUser, HttpSession session) {
        User userNow = (User) session.getAttribute("user1");
        if(!userNow.getPassword().equals(myUser.getPassword2())){
            ResponseEntity<User> entity = new ResponseEntity<>(501, "密码不正确！");
            entity.setData(userNow);
            return entity;
        }
        boolean change = false;
        //修改验证账户重复
        String account = myUser.getAccount();
        if (account != null) {
            User userExist = userService.getByAccount(account);
            if (userExist != null) {
                ResponseEntity<User> entity = new ResponseEntity<>(501, "用户名存在！");
                entity.setData(userNow);
                return entity;
            }
            userNow.setAccount(account);
            change = true;
        }
        //修改验证邮箱重复
        String mail = myUser.getEmail();
        if (mail != null) {
            User byEmail = userService.getUserByEmail(mail);
            if (byEmail != null) {
                ResponseEntity<User> entity = new ResponseEntity<>(501, "邮箱存在！");
                entity.setData(userNow);
                return entity;
            }
            userNow.setEmail(mail);

            change = true;
        }
        if (change) {
            userService.updateById(userNow);
            return new ResponseEntity<>(userNow);
        } else {
            return ResponseEntity.FIAL;
        }
    }
}

